/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myjme.tin.core;

import com.jme3.math.FastMath;
import com.jme3.math.Matrix3f;
import com.jme3.math.Vector3f;

/**
 *
 * @author Administrator
 */
public class Tin {
    //判断是否在平面内
    public boolean isInSurface(Vector3f p1,Vector3f p2,Vector3f p3,Vector3f p4){
        boolean flags=true;
        
        Vector3f CHAJI=new Vector3f();
        Vector3f AB=new Vector3f();
        Vector3f AC=new Vector3f();
        Vector3f AP=new Vector3f();
        
        AB=p2.subtract(p1);
        AC=p3.subtract(p1);
        CHAJI=AB.cross(AC);
        
        AP=p4.subtract(p1);
        
        if (CHAJI.dot(AP)==0) {
            flags=true;
        }else
            flags=false;
        
        return flags;
    }
    //判断是否在圆内
    public boolean inInCircle(Vector3f p1,Vector3f p2,Vector3f p3,Vector3f p4){
        
        float res[]=caculateCircle(p1, p2, p3);
        
        Vector3f center=new Vector3f(res[0], res[1], res[2]);
        
        //判断距离
        Vector3f po=p4.subtract(center);
        float leng=po.length();
        
        if (leng<res[3]) {
            return true;
        }
        
        return false;
    }
    //判断是否在三角形内
    public boolean isInTriangle(Vector3f p1,Vector3f p2,Vector3f p3,Vector3f p4){

        //找到所有向量
        Vector3f PA=p1.subtract(p4);
        Vector3f PB=p2.subtract(p4);
        Vector3f PC=p3.subtract(p4);
        Vector3f AB=p2.subtract(p1);
        Vector3f BC=p3.subtract(p2);
        Vector3f CA=p1.subtract(p3);
        
        //PA  AB -- CA  AB ;    PB  BC -- AB  BC ;  PC  CA -- BC CA 
        Vector3f NormalPA=new Vector3f();
        Vector3f NormalA=new Vector3f();
        Vector3f NormalPB=new Vector3f();
        Vector3f NormalB=new Vector3f();
        Vector3f NormalPC=new Vector3f();
        Vector3f NormalC=new Vector3f();
        
        //获得向量点乘积
        Vector3f paCross=PA.cross(AB);
        Vector3f aCross=CA.cross(AB);
        Vector3f pbCross=PB.cross(BC);
        Vector3f bCross=AB.cross(BC);
        Vector3f pcCross=PC.cross(CA);
        Vector3f cCross=BC.cross(CA);
        double aDot=paCross.dot(aCross);
        double bDot=pbCross.dot(bCross);
        double cDot=pcCross.dot(cCross);
        
        if (aDot<=0||bDot<=0||cDot<=0) {
            return false;
        }
        
        return true;
    }
    //核心计算
    public float [] caculateCircle(Vector3f p1,Vector3f p2,Vector3f p3){
        float A11,A12,A13,A21,A22,A23,A31,A32,A33;
        float T11,T12,T13,T21,T22,T23,T31,T32,T33;
        float B1,B2,B3;
        
        Vector3f po=new Vector3f();
        Vector3f AB=new Vector3f(),AC=new Vector3f(),N=new Vector3f();//这里分别是AB，AC，法向向量N
        
        Matrix3f MA = new Matrix3f(),MAT=new Matrix3f();
        Vector3f BV = new Vector3f();
        
        float r=0;//这是半径
        
        
        //找出法向量
        AB=p2.subtract(p1);
        ////System.out.println("AB"+AB.toString());
        AC=p3.subtract(p1);
        ////System.out.println("AC"+AC.toString());
        N=AB.cross(AC);
        ////System.out.println("N"+N.toString());
        //初始化九个点
        A11=N.x;
        A12=N.y;
        A13=N.z;
        A21=(-2*p1.x)+2*p2.x;
        A22=(-2*p1.y)+2*p2.y;
        A23=(-2*p1.z)+2*p2.z;
        A31=(-2*p1.x)+2*p3.x;
        A32=(-2*p1.y)+2*p3.y;
        A33=(-2*p1.z)+2*p3.z;
        //初始化等号右边的点
        B1=N.x*p1.x+N.y*p1.y+N.z*p1.z;
        B2=p2.x*p2.x+p2.y*p2.y+p2.z*p2.z-p1.x*p1.x-p1.y*p1.y-p1.z*p1.z;
        B3=p3.x*p3.z+p3.y*p3.y+p3.z*p3.z-p1.x*p1.x-p1.y*p1.y-p1.z*p1.z;
        BV.set(B1, B2, B3);
        ////System.out.println("B1 "+B1+"  B2  "+B2+"  B3  "+B3);
        //MA找到九个点
        MA.set(0, 0, A11);
        MA.set(0, 1, A12);
        MA.set(0, 2, A13);
        MA.set(1, 0, A21);
        MA.set(1, 1, A22);
        MA.set(1, 2, A23);
        MA.set(2, 0, A31);
        MA.set(2, 1, A32);
        MA.set(2, 2, A33);
        ////System.out.println("MA"+MA.toString());
        //MA逆矩阵
        MA.invert(MAT);
        ////System.out.println("MAt"+MAT.toString());
        po=MAT.mult(BV);
        r= po.subtract(p1).length();

        float[] result={po.x,po.y,po.z,r};
        System.out.println(po.x+"    "+po.y+"    "+po.z+"   r  "+r);
        return  result;
    }
}
